package com.platform.api;

import com.aliyun.oss.OSSClient;
import com.platform.annotation.IgnoreAuth;
import com.platform.util.AliOSSClientConstants;
import com.platform.util.AliyunOSSClientUtil;
import com.platform.oss.OSSFactory;
import com.platform.util.ApiBaseAction;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/upload")
public class ApiUploadController extends ApiBaseAction {

    /**
     * 上传文件
     */
    @IgnoreAuth
    @RequestMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) throws Exception {
        //上传文件
        String contentType = file.getContentType();
        String url;
        if (!contentType.contains("video")) {
            url = OSSFactory.build().upload(file);
        } else {
            byte[] b1 = file.getBytes();
            System.out.println(file.getContentType());
            OSSClient ossClient = AliyunOSSClientUtil.getOSSClient();
            String path = AliyunOSSClientUtil.uploadByteVideoOSS(ossClient, b1, AliOSSClientConstants.BACKET_NAME);
            url = "https://chunya-static.oss-cn-shenzhen.aliyuncs.com/" + path;
        }

        return toResponsSuccess(url);
    }


}